package com.example.myapplication.srt;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: rongfeng
 * @date: 2021/6/4
 * @description
 */
public class SubtitlesFactory {

    public static void main(String[] args) {
        SubtitlesFactory subtitlesFactory = new SubtitlesFactory();
        subtitlesFactory.folderMethod5(SubtitlesFactory.mPath);

    }


    public static String mPath = "D:\\BaiduNetdiskDownload\\peppa pig3\\json";

    public void folderMethod5(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        folderMethod5(file2.getAbsolutePath());
                    } else {
                        String name = file2.getName();

                        if (name.contains("_zh.txt")) {
                            SubtitlesDataCoding subtitlesDataCoding = new SubtitlesDataCoding();

                            ArrayList<SubtitlesModel> models = subtitlesDataCoding.readFile(file2.getPath(), false);

                            SubtitlesDataCoding subtitlesDataCoding2 = new SubtitlesDataCoding();

                            String enname = name.replace("_zh", "");
                            File file5 = new File(mPath + "\\" + enname);
                            ArrayList<SubtitlesModel> models2 = subtitlesDataCoding2.readFile(file5.getPath(), true);
                            hebin(models, models2, name.replace("_zh.txt", ""));
                        }


                    }
                }
            }
        } else {
//            System.out.println("文件不存在!");
        }
    }

    public void saveFile(List<SubtitlesModel> models, String name) {
        File file = new File(mPath + "\\" + name + ".json");
        try {
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            String json = GsonUtil.toJson(models);
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(json);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    void hebin(ArrayList<SubtitlesModel> models, ArrayList<SubtitlesModel> models2, String name) {
        try {
            for (int i = 0; i < models2.size(); i++) {
                SubtitlesModel subtitlesModel = models2.get(i);
                SubtitlesModel subtitlesModel2 = models.get(i);
                subtitlesModel.contextC = subtitlesModel2.contextC;
                if(subtitlesModel.star != subtitlesModel2.star){
                    System.out.println(name + " "+ i);
                }
            }
            saveFile(models2, name);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(name);
        }
    }

}
